package com.kucai.yifaqapp.ui.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bumptech.glide.Glide;
import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.Pay_DanYICZBean;
import com.kucai.yifaqapp.model.network.entity.res.z_ysf_ty;
import com.kucai.yifaqapp.util.SYSOUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by admin on 2018/12/4.
 */

public class Z_ysfctivity extends BaseActivitySimple {
    TextView remark2;
    TextView payee;
    TextView remark;
    TextView dsplay;
    Button next;
    private List<String> mdatas = new ArrayList<>();
    Pay_DanYICZBean bean;
    RelativeLayout back;
    ImageView dizhi;
    LinearLayout skinbg;
    private OptionsPickerView pvOptions;
    Button copyaccent;
    Button copyname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        back = findViewById(R.id.back);
        dizhi = findViewById(R.id.dizhi);
        skinbg = findViewById(R.id.skinbg);
        copyname = findViewById(R.id.copyname);
        copyaccent = findViewById(R.id.copyaccent);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        copyname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager copy = (ClipboardManager) Z_ysfctivity.this
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                copy.setText(remark2.getText().toString());
                Toast.makeText(getApplicationContext(), "复制成功", Toast.LENGTH_SHORT).show();
            }
        });
        copyaccent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager copy = (ClipboardManager) Z_ysfctivity.this
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                copy.setText(payee.getText().toString());
                Toast.makeText(getApplicationContext(), "复制成功", Toast.LENGTH_SHORT).show();
            }
        });
        try {
            initView();
            initData();
        } catch (Exception e) {

        }
    }

    private void initView() {
        remark2 = findViewById(R.id.remark2);
        payee = findViewById(R.id.payee);
        remark = findViewById(R.id.remark);
        dsplay = findViewById(R.id.dsplay);
        next = findViewById(R.id.next);
    }

    @Override
    protected int setContentView() {
        return R.layout.z_ysf;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    private void initData() {
        HttpJsonUtils.getHttpApiInstance()
                .getSingleCZ(HttpAdress.getuseAdress() + "api/m/user/getPay", "ysf", Info.user.token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Pay_DanYICZBean>(this) {
                    @Override
                    protected void onCustomNext(Pay_DanYICZBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                bean = resBanner;
                                for (int i = 0; i < resBanner.getRech_types().size(); i++) {
                                    mdatas.add(resBanner.getRech_types().get(i).getDsplay());
                                }
                                pvOptions = new OptionsPickerView.Builder(Z_ysfctivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                                    @Override
                                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                                        remark2.setText(bean.getRech_types().get(options1).getPayeeName() + "");
                                        payee.setText(bean.getRech_types().get(options1).getPayee() + "");
                                        remark.setText(bean.getRech_types().get(options1).getRemark() + "");
                                        dsplay.setText(mdatas.get(options1) + "");
                                        Glide.with(Z_ysfctivity.this).load(bean.getRech_types().get(options1).getQrcode()).into(dizhi);
                                        z_ysf_ty.cfgId = bean.getRech_types().get(options1).getId() + "";
                                        z_ysf_ty.yinghang = mdatas.get(options1) + "";
                                        z_ysf_ty.wangdian = bean.getRech_types().get(options1).getPageDesc() + "";
                                        z_ysf_ty.xingming = bean.getRech_types().get(options1).getPayeeName() + "";
                                        z_ysf_ty.kahao = bean.getRech_types().get(options1).getPayee() + "";
                                        z_ysf_ty.max = bean.getRech_types().get(options1).getMax_money() + "";
                                        z_ysf_ty.min = bean.getRech_types().get(options1).getMin_money() + "";
                                    }
                                })
                                        .setSubCalSize(20)//确定和取消文字大小
                                        .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                                        .setCancelColor(Color.BLUE)//取消按钮文字颜色
                                        .setTextColorCenter(Color.BLACK)//设置选中项的颜色
                                        .setSelectOptions(0)
                                        .build();

                                pvOptions.setPicker(mdatas);
                                dsplay.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        pvOptions.show();
                                    }
                                });
                                //给一个默认显示
                                try {
                                    remark2.setText(bean.getRech_types().get(0).getPayeeName() + "");
                                    payee.setText(bean.getRech_types().get(0).getPayee() + "");
                                    remark.setText(bean.getRech_types().get(0).getRemark() + "");
                                    dsplay.setText(mdatas.get(0) + "");
                                    Glide.with(Z_ysfctivity.this).load(bean.getRech_types().get(0).getQrcode()).into(dizhi);
                                    z_ysf_ty.cfgId = bean.getRech_types().get(0).getId() + "";
                                    z_ysf_ty.yinghang = mdatas.get(0) + "";
                                    z_ysf_ty.wangdian = bean.getRech_types().get(0).getPageDesc() + "";
                                    z_ysf_ty.xingming = bean.getRech_types().get(0).getPayeeName() + "";
                                    z_ysf_ty.kahao = bean.getRech_types().get(0).getPayee() + "";
                                    z_ysf_ty.max = bean.getRech_types().get(0).getMax_money() + "";
                                    z_ysf_ty.min = bean.getRech_types().get(0).getMin_money() + "";
                                } catch (Exception e) {

                                }
                            } else {
                                Toast.makeText(Z_ysfctivity.this, resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            Toast.makeText(Z_ysfctivity.this, "异常错误，请重新操作", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dsplay.getText().toString().equals("请点击选择支付账号")) {
                    Toast.makeText(Z_ysfctivity.this, "请点击选择支付账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(Z_ysfctivity.this, Z_ysfctivitySecond.class);
                Bundle bundle = new Bundle();
                bundle.putString("remark", remark.getText().toString() + "");
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        });
    }
}
